package com.obradovicnjegovan.webprodavnica.entities;

import java.beans.Transient;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.Sha1Hash;
import org.apache.shiro.util.ByteSource;
import org.apache.tapestry5.beaneditor.NonVisual;
import org.hibernate.annotations.CollectionOfElements;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.annotations.NaturalId;

@SuppressWarnings("serial")
@Entity
public class Korisnik extends OpstiDomenskiObjekat {
	private String ime;
	private String prezime;
	private String eMail;
	private String telefon;
	private String adresa;
	private String drzava;
	private String grad;
	private Korpa korpa;
	private Set<Narudzbenica> narudzbenice;
	
	private String username;
	private String encodedPassword;
	private byte[] passwordSalt;
	private Set<TipKorisnika> roles = new HashSet<TipKorisnika>();

	public String getIme() {
		return ime;
	}

	public void setIme(String ime) {
		this.ime = ime;
	}

	public String getPrezime() {
		return prezime;
	}

	public void setPrezime(String prezime) {
		this.prezime = prezime;
	}

	public String geteMail() {
		return eMail;
	}

	public void seteMail(String eMail) {
		this.eMail = eMail;
	}

	@OneToOne(cascade = CascadeType.ALL)
	public Korpa getKorpa() {
		return korpa;
	}

	public void setKorpa(Korpa korpa) {
		this.korpa = korpa;
	}

	@OneToMany(cascade = CascadeType.ALL)
	@LazyCollection(LazyCollectionOption.FALSE)
	public Set<Narudzbenica> getNarudzbenice() {
		return narudzbenice;
	}

	public void setNarudzbenice(Set<Narudzbenica> narudzbenice) {
		this.narudzbenice = narudzbenice;
	}

	public String getTelefon() {
		return telefon;
	}

	public void setTelefon(String telefon) {
		this.telefon = telefon;
	}

	public String getAdresa() {
		return adresa;
	}

	public void setAdresa(String adresa) {
		this.adresa = adresa;
	}

	public String getDrzava() {
		return drzava;
	}

	public void setDrzava(String drzava) {
		this.drzava = drzava;
	}

	public String getGrad() {
		return grad;
	}

	public void setGrad(String grad) {
		this.grad = grad;
	}

	
	
	@NaturalId
	@Column(unique = true)
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
	@NonVisual
	public String getEncodedPassword() {
		return encodedPassword;
	}

	public void setEncodedPassword(String encodedPassword) {
		this.encodedPassword = encodedPassword;
	}
	@NonVisual
	@Column(length = 128)
	public byte[] getPasswordSalt() {
		return passwordSalt;
	}

	public void setPasswordSalt(byte[] passwordSalt) {
		this.passwordSalt = passwordSalt;
	}
	
	@Override
	public int hashCode() {
		return username == null ? 0 : username.hashCode();
	}
	
	@Transient
	public String getPassword() {
		return "";
	}

	public void setPassword(String password) {
		if (password != null && !password.equals(encodedPassword) && !"".equals(password)) {
			ByteSource saltSource = new SecureRandomNumberGenerator().nextBytes();
			this.passwordSalt = saltSource.getBytes();
			this.encodedPassword = new Sha1Hash(password, saltSource).toString();
		}
	}
	
	@Override
	public String toString() {
		return "Korisnik " + username;
	}
	@CollectionOfElements(targetElement = TipKorisnika.class)
	public Set<TipKorisnika> getRoles() {
		return roles;
	}

	public void setRoles(Set<TipKorisnika> roles) {
		this.roles = roles;
	}
	
}
